package com.uchengwang.ui.cm_ui.cycle.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.RyGroupBean;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import io.rong.imlib.model.Conversation;

/**
 * Created by U城网 on 2018/1/22.
 * 社圈群组
 */

public class CycleGroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<RyGroupBean> list = new ArrayList<>();

    public CycleGroupAdapter(Context context){
        this.context = context;
    }

    public void setData(List<RyGroupBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<RyGroupBean> getData(){
        return list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout_view, parent, false);
        return new GroupVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        GroupVH holder = (GroupVH) h;
        final RyGroupBean bean = list.get(position);
        ImageManager.getInstance().loadImage(context, holder.apply_image, bean.getGroupLogo(), ImageManager.ImageType.CIRCLE);
        holder.apply_name.setText(bean.getGroupName());
        if(bean.getUni() != null){
            holder.apply_school.setText(bean.getUni().getUniName());
        }

        if (bean.isMember()){
            //已加群
            holder.apply_atten.setText("聊天");
            holder.apply_atten.setBackgroundResource(R.drawable.selector_chat);
            holder.apply_atten.setTextColor(context.getResources().getColor(R.color.register_next_selector));
        }else {
            if (bean.getApplyStatus() >=0){
                //说明之前有过申请加群的操作
                holder.apply_atten.setText("验证中");
                holder.apply_atten.setBackgroundResource(R.drawable.selector_no_atten);
                holder.apply_atten.setTextColor(context.getResources().getColor(R.color.delete_selector));
            }else {
                holder.apply_atten.setText("加群");
                holder.apply_atten.setBackgroundResource(R.drawable.selector_friends);
                holder.apply_atten.setTextColor(context.getResources().getColor(R.color.register_user_selector));
            }
        }

        holder.apply_atten.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.isMember()){
                    UCommonUtil.getUtil().toChatActiviy(context, bean.getId(), Conversation.ConversationType.GROUP, bean.getGroupName());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class GroupVH extends RecyclerView.ViewHolder {
        private View itemView;
        private ImageView apply_image;
        private TextView apply_name, apply_type, apply_school;
        private Button apply_atten;

        public GroupVH(View view) {
            super(view);
            AutoUtils.autoSize(view);
            itemView = view;

            apply_image = (ImageView) view.findViewById(R.id.apply_image);
            apply_name = (TextView) view.findViewById(R.id.apply_name);
            apply_type = (TextView) view.findViewById(R.id.apply_type);
            apply_school = (TextView) view.findViewById(R.id.apply_school);

            apply_atten = (Button) view.findViewById(R.id.apply_atten);
            apply_atten.setVisibility(View.VISIBLE);


        }
    }
}
